import axios from 'axios'
import {ElMessage} from 'element-plus'

axios.defaults.baseURL = 'http://localhost:8080/'

//配置拦截器，统一处理请求
axios.interceptors.request.use(
    (config) => {
        //在请求头中添加token
        const token = localStorage.getItem("token");
        console.log(token);
        if (token) {
            config.headers['token'] = token
        }
        return config;
    },
    (error) => {
        return Promise.reject(error.response.data.msg);       // 对请求错误做些什么
    }
);

//配置拦截器，统一处理响应
axios.interceptors.response.use(
    (response) => {
        console.log(response.config.url);
        if (response.config.url === '/user/getCaptcha') {
            // 这里可以根据需要处理 captcha 的响应
            // 例如获取响应头或处理二进制数据等
            return response; // 返回整个 response 对象而不是 response.data
        }
        ElMessage.success(response.data.msg)
        return response.data
    },
    (error) => {
        ElMessage.error(error.response.data.msg)
    }
);
export default axios;
